package model;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import exception.BuildNameNotSetException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.ini4j.Wini;

@Singleton
public class ConfigManager {

    private  Wini cfg;
    private  final String FILENAME = "C:\\Documents and Settings\\dongxu\\My Documents\\NetBeansProjects\\test\\cfg\\default.cfg";
    private  final String FILENAME_BACK1 = "C:\\Users\\think\\Documents\\NetBeansProjects\\test\\cfg\\default.cfg";
    private final String MACHINE_CFG = "C:\\Documents and Settings\\dongxu\\My Documents\\NetBeansProjects\\test\\cfg\\machines.cfg";
    private final String MACHINE_CFG2 = "C:\\Users\\think\\Documents\\NetBeansProjects\\test\\cfg\\machines.cfg";
    ConfigLoader configLoader;

    @Inject
    public ConfigManager(ConfigLoader configLoader) {
        this.configLoader = configLoader;
        File[] configFiles = new File[]{new File(FILENAME), new File(FILENAME_BACK1)};
        cfg = this.configLoader.loadConfig(configFiles);
    }
    
    private  String build;
    private String installerPath;
    private  String machine;

    public  boolean reload() {
        cfg = null;
        cfg = configLoader.loadConfig(new File[]{new File(FILENAME), new File(FILENAME_BACK1)});
        return cfg == null ? false : true;
    }

    public  int getNewBuildWatchFrequency() {
        String frequency = cfg.get("server", "new_build_watch_frequency").trim();
        return new Integer(frequency).intValue();
    }

    public  String getCurrentInstallerArchive() throws BuildNameNotSetException {
        return getInstallerPath() + "\\" + getBuildName() + ".zip";
    }

    public  String getUsageFilePath() {
        return cfg.get("server", "usage_file");
    }

    public  int getShowCmdHistoryCount() {
        String count = cfg.get("server", "show_command_history").trim();
        return new Integer(count).intValue();
    }

    public  int getShowTestResultHistoryCount() {
        String count = cfg.get("server", "show_testresult_history").trim();
        return new Integer(count).intValue();
    }

    public String getBiuldReleasePath() {
        return cfg.get("server", "build_release_path");
    }

    public void setMachine(String m) {
        machine = m;
    }

    public String getMachine(){
        return machine;
    }

    private  String getCfgPath() throws IOException {
        File pwd = new File(".");
        return pwd.getCanonicalPath() + "\\cfg\\";
    }

    public  String getBuildName() throws BuildNameNotSetException {
        if (build == null) {
            throw new BuildNameNotSetException();
        }
        return build;
    }

//    public static boolean reload() {
//        try {
//            cfg = new Wini(new File(getCfgPath()+"default.cfg"));
//            return true;
//        } catch (IOException ex) {
//            Logger.getLogger(VMMachineFactory.class.getName()).log(Level.SEVERE, null, ex);
//            return false;
//        }
//    }
    public  String getLogPath() {
        return cfg.get("server", "log_path");
    }

    public  String getProjectPath() {
        return cfg.get("server", "project_path");
    }

    public  String getServerHostName() {
        return cfg.get("server", "hostname");
    }

    public  String getInstallerPath() {
        return installerPath;
//        return cfg.get("server", "installer_path");
    }

    public void setInstallerPath(String path){
        this.installerPath = path;
    }

    public  String getVMMachineCfgFile() {
        File[] configFiles = new File[]{new File(MACHINE_CFG), new File(MACHINE_CFG2)};
        cfg = this.configLoader.loadConfig(configFiles);
        return cfg.get("server", "vm_cfg_file");
//        return "C:\\Documents and Settings\\dongxu\\My Documents\\NetBeansProjects\\test\\cfg\\machines.cfg";
    }

    public  String isStartedHandleName() {
        return cfg.get("handleName", "is_vm_started");
    }

    public  String getSnapshotsHandleName() {
        return cfg.get("handleName", "get_snapshots");
    }

    public  String revertToSnapshotHandleName() {
        return cfg.get("handleName", "revert_vm");
    }

    public  void setBuildName(String buildName) {
        build = buildName;
    }

    public  String startVMHandleName() {
        return cfg.get("handleName", "start_vm");
    }

    public  String runTSHandleName() {
        return cfg.get("handleName", "run_script");
    }

    public  String getProjectArchiveName() {
        return cfg.get("server", "project_archive_name");
    }

    public  String getZipHandleName() {
        return cfg.get("handleName", "zip");
    }

    public  String getFSHandleName() {
        return cfg.get("handleName", "copy_file");
    }

    public  String getUnzipHandleName() {
        return cfg.get("handleName", "unzip");
    }

    public  String getTakeSnapshotHandleName() {
        return cfg.get("handleName", "take_snapshot");
    }

    public  String getStopVMHandleName() {
        return cfg.get("handleName", "stop_vm");
    }

    public  String getListHandlesHandleName() {
        return cfg.get("handleName", "list_handles");
    }

    public InputStream getResource(String file){
        return this.getClass().getResourceAsStream(file);
    }

    public static void main(String[] args) {
        InputStream is = new ConfigManager(new ConfigLoader()).getResource("machines.cfg");
        System.out.println(is);
    }
}
